//
//  MapController.m
//  VC
//
//  Created by apple on 15/10/10.
//  Copyright © 2015年 Leaning iOS Development. All rights reserved.
//

#import "WXDataService.h"
#import "Unit.h"
@implementation WXDataService

//网络请求
+ (AFHTTPRequestOperation *)requestWithURL:(NSString *)url
                                    params:(NSMutableDictionary *)params
                                httpMethod:(NSString *)httpMethod
                                     block:(CompletionLoadHandle)block
                                 failblock:(requestfailblock)failblock
{
    if (params == nil) {
        params = [NSMutableDictionary dictionary];
    }
    NSLog(@"params = %@", params);
    //1.拼接URL
    NSString *urlStr = [NSString stringWithFormat:@"%@%@",BASE_URL,url];
    urlStr = [urlStr stringByReplacingOccurrencesOfString:@" " withString:@""];
//    NSMutableString *urlStr = [NSMutableString stringWithFormat:@"%@%@",REQUEST_URL_ROOT,url];
    NSString *Cookie = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"Cookie"]];
    NSLog(@"Cookie = %@", Cookie);
    AFHTTPRequestOperation *operation = nil;
    
    AFHTTPRequestOperationManager *oprationManager=[AFHTTPRequestOperationManager manager];
    //转换格式
    oprationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", nil];
    oprationManager.responseSerializer = [AFJSONResponseSerializer serializer];
    oprationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    oprationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    if (Cookie) {
//        [oprationManager.requestSerializer setValue:[[NSUserDefaults standardUserDefaults] objectForKey:@"Cookie"] forHTTPHeaderField:@"Cookie"];
    }
    //    [oprationManager.requestSerializer setValue:@"JSESSIONID=52B6907D3E78AF3ADB44B7C70F8E3FAB; Path=/aos/; HttpOnly" forHTTPHeaderField:@"Cookie"];
    //    [oprationManager.requestSerializer setValue:@"JSESSIONID=52B6907D3E78AF3ADB44B7C70F8E3FAB; Path=/aos/; HttpOnly" forHTTPHeaderField:@"Cookie"];
    NSLog(@"urlstr = %@", urlStr);
    [oprationManager.requestSerializer setValue:@"JSESSIONID=52B6907D3E78AF3ADB44B7C70F8E3FAB; Path=/aos/; HttpOnly" forHTTPHeaderField:@"Cookie"];
    [oprationManager POST:urlStr parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject){
        
        
        NSLog(@"%@",Cookie);
        
        if (!Cookie || [Cookie isEqualToString:@"(null)"]) {
            [[NSUserDefaults standardUserDefaults] setObject:[operation.response.allHeaderFields objectForKey:@"Set-Cookie"] forKey:@"Cookie"];
        }
        
        NSString *string = operation.responseString;
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = nil;
        if (data!= nil) {
            dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        }
        if (block != nil) {
            NSString *success = [NSString stringWithFormat:@"%@", [dic objectForKey:@"success"]];
            NSString *msg = [NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]];
            NSDictionary *infor = [dic objectForKey:@"infor"];
            NSLog(@"success--msg = %@----%@", success, msg);
            block(infor, success, msg);
        }
    }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"-----Error = %@", error);
        
        NSLog(@"cookie===%@",Cookie);
        
        failblock(operation,error);
    }];
    
    //设置返回数据的解析方式
    operation.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
    
    return operation;
}

//上传图片
+ (AFHTTPRequestOperation *)postImageWithURL:(NSString *)url
                               imagePathFile:(NSString *)fullPathToFile
                                      params:(NSMutableDictionary *)params
                                  httpMethod:(NSString *)httpMethod
                                       block:(CompletionLoadHandle)block
{
    if (params == nil) {
        params = [NSMutableDictionary dictionary];
    }
    NSString *urlstring = [NSString stringWithFormat:@"%@%@", BASE_URL, url];
    
    AFHTTPRequestOperation *operation = nil;
    
    AFHTTPRequestOperationManager *oprationManager = [AFHTTPRequestOperationManager manager];
    oprationManager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", nil];
    oprationManager.responseSerializer = [AFJSONResponseSerializer serializer];
    oprationManager.requestSerializer = [AFHTTPRequestSerializer serializer];
    oprationManager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *Cookie = [NSString stringWithFormat:@"%@", [[NSUserDefaults standardUserDefaults] objectForKey:@"Cookie"]];
    [oprationManager.requestSerializer setValue:Cookie forHTTPHeaderField:@"Cookie"];
    [oprationManager POST:urlstring parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        if (!Cookie || [Cookie isEqualToString:@"(null)"]) {
            [[NSUserDefaults standardUserDefaults] setObject:[operation.response.allHeaderFields objectForKey:@"Set-Cookie"] forKey:@"Cookie"];
        }
        UIImage *image = [UIImage imageWithContentsOfFile:fullPathToFile];
        NSData* data = UIImageJPEGRepresentation(image, 0.001);
        [formData appendPartWithFileData:data name:@"file_" fileName:@"image.jpeg" mimeType:@"image/jpeg"];
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = operation.responseString;
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary *dic = nil;
        if (data!= nil) {
            dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        }
        NSLog(@"Success: %@", dic);
        if (block != nil) {
            NSString *success = [NSString stringWithFormat:@"%@", [dic objectForKey:@"success"]];
            NSString *msg = [NSString stringWithFormat:@"%@", [dic objectForKey:@"msg"]];
            NSDictionary *infor = [dic objectForKey:@"infor"];
            block(infor, success, msg);
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
    
    //设置返回数据的解析方式
    operation.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingMutableContainers];
    
    return operation;
}

@end
